package main.java.FSMDE.persistence;

import java.util.ArrayList;
import java.util.UUID;

import main.java.ERMDE.models.Diagram;
import main.java.FSMDE.models.ComplexComponent;
import main.java.FSMDE.models.Component;
import main.java.FSMDE.models.ComponentCollection;
import main.java.FSMDE.models.ComponentType;
import main.java.FSMDE.models.FactoryComponent;
import main.java.FSMDE.models.RelatedComponent;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class ComponentXmlManager {

	private ArrayList<Diagram> ermDiagrams;

	public Component getItemFromElement(Element elemComponent)
			throws IllegalArgumentException, Exception {
		UUID id = UUID.fromString(elemComponent.getAttribute("id"));
		String name = elemComponent.getAttribute("name");
		String genericType = "";
		ComponentType type = ComponentType.valueOf(elemComponent
				.getAttribute("type"));
		if (type == ComponentType.Generic)
			genericType = elemComponent.getAttribute("genericType");
		if (elemComponent.getElementsByTagName("components").getLength() != 0) {
			ComponentCollection complexCollection = new ComponentCollectionXmlManager(
					ermDiagrams).getItemFromXmlElement((Element) elemComponent
					.getElementsByTagName("components").item(0));
			ComplexComponent complexComp = (ComplexComponent) FactoryComponent
					.create(name, type);
			complexComp.setId(id);
			complexComp.setGenericType(genericType);
			complexComp.setComponents(complexCollection);
			return complexComp;
		}
		Component component = FactoryComponent.create(name, type);
		component.setId(id);
		component.setGenericType(genericType);
		return component;
	}

	public Element getElementFromItem(Component comp, Document doc) {
		Element compElement = doc.createElement("component");
		compElement.setAttribute("name", comp.getName());
		compElement.setAttribute("id", comp.getId().toString());
		compElement.setAttribute("type", comp.getType().name());
		if (comp.getGenericType() != "")
			compElement.setAttribute("genericType", comp.getGenericType());
		// for complex
		if (comp.isComplex()) {
			compElement.appendChild(new ComponentCollectionXmlManager(
					ermDiagrams).getElementFromItem(
					((ComplexComponent) comp).getComponents(), doc));
			if (comp instanceof RelatedComponent) {
				Element relationsElement = doc.createElement("fsmErmRelations");
				for (Component relatedComponent : ((RelatedComponent) comp)
						.getRelatedComponents()) {
					Element relationElement = doc.createElement("relation");
					relationElement.setAttribute("componentId",
							relatedComponent.getId().toString());
					relationElement.setAttribute(
							"ermDiagramId",
							((RelatedComponent) comp)
									.getRelatedErmModel(relatedComponent)
									.getDiagram().getId().toString());
					relationElement.setAttribute(
							"entityId",
							((RelatedComponent) comp)
									.getRelatedErmModel(relatedComponent)
									.getEntity().getId().toString());
					relationElement.setAttribute(
							"attributeId",
							((RelatedComponent) comp)
									.getRelatedErmModel(relatedComponent)
									.getAttribute().getId().toString());

					relationsElement.appendChild(relationElement);
				}
				compElement.appendChild(relationsElement);
			}
		}

		return compElement;
	}

	public void setErmDiagrams(ArrayList<Diagram> ermDiagrams) {
		this.ermDiagrams = ermDiagrams;
	}

}
